package com.pengheng.shardingjdbc.shard.precise;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

public class OrderPreciseStrategy implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(preciseShardingValue.getValue());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String tableValue = year + "_" + month;
//        LocalDateTime value = preciseShardingValue.getValue();
//        String tableValue = value.getYear() + "_" + value.getMonthValue();
        return collection.stream().filter(item -> item.endsWith(tableValue)).findFirst().orElseThrow(() -> new RuntimeException("没匹配到实绩表"));
    }
}
